<template>
  <div>

    <el-form size="small" :inline="true" :model="searchForm" class="demo-form-inline">
      <el-form-item label="商品分类">
        <el-cascader v-model="searchForm.category_id" :options="categoryList" @change="handleChange1"
          :props="{ label: 'name', value: 'category_id', children: 'child' }" clearable></el-cascader>
      </el-form-item>
      <el-form-item label="商品名称">
        <el-input size="small" v-model="searchForm.keys" placeholder="请输入商品名称"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button size="small" type="primary" icon="el-icon-search" @click="onSubmit">查询</el-button>
      </el-form-item>
    </el-form>
    <!--添加等级-->
    <el-tabs v-model="searchForm.picks" @tab-click="handleClick">
      <el-tab-pane label="全部" name=""></el-tab-pane>
      <el-tab-pane label="已选入" name="1"></el-tab-pane>
      <el-tab-pane label="未选入" name="2"></el-tab-pane>
    </el-tabs>
    <div class="common-level-rail">
      <el-button size="small" type="primary" @click="addClick1" icon="el-icon-plus"
        v-auth="'/wps/product/pl_add'">批量添加</el-button>
    </div>
    <!--内容-->
    <div class="product-content">
      <div class="table-wrap">
        <el-table size="small" :data="tableData" border style="width: 100%" v-loading="loading"
          @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55" :selectable="checkSelectable"></el-table-column>
          <el-table-column prop="product_name" label="产品" width="400px">
            <template slot-scope="scope">
              <div class="product-info">
                <div class="pic"><img v-img-url="scope.row.details.c_goods_image" alt="" /></div>
                <div class="info">
                  <div class="name">{{ scope.row.details.c_goods_name }}</div>
                  <div class="price">¥ {{ scope.row.details.c_sale_price }}</div>
                </div>
              </div>
            </template>
          </el-table-column>
          <el-table-column prop="price" label="铺货价	"></el-table-column>
          <el-table-column prop="details.c_sale_price" label="建议售价"></el-table-column>
          <el-table-column prop="details.c_goods_stock_valid" label="库存"></el-table-column>
          <el-table-column prop="check_sta" label="状态"></el-table-column>
          <el-table-column fixed="right" label="操作">
            <template slot-scope="scope">
              <div class="table-btn-column">
                <el-button @click="addClick(scope.row)" type="text" size="small"
                  v-auth="'/wps/product/add'">添加商品</el-button>
                <el-button @click="detailClick(scope.row)" type="text" size="small"
                  v-auth="'/wps/product/info'">详情</el-button>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
    <!--分页-->
    <div class="pagination">
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" background
        :current-page="curPage" :page-size="pageSize" layout="total, prev, pager, next, jumper"
        :total="totalDataNumber"></el-pagination>
    </div>
    <!-- 添加云仓 -->
    <el-dialog title="添加云仓商品" :visible.sync="dialogFormVisible" @close="onclose">
      <el-form :model="addForm" size="small" ref="addForm" label-width="100px">
        <el-form-item label="溢价比例" :rules="[{ required: true, message: '请输入溢价比例' }]" prop="rate">
          <el-input v-model="addForm.rate" placeholder="请输入溢价比例">
            <template slot="append">%</template>
          </el-input>
        </el-form-item>
        <el-form-item label="所属分类" :rules="[{ required: true, message: '请选择所属分类' }]" prop="category_id">
          <el-cascader v-model="addForm.category_id" :options="categoryList" @change="handleChange"
            :props="{ label: 'name', value: 'category_id', children: 'child' }"></el-cascader>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="add">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import PorductApi from '@/api/product.js';
import SettingApi from '@/api/setting.js';
import { createLogger } from 'vuex';
export default {
  data() {
    return {
      categoryList: [],
      /*是否正在加载*/
      loading: true,
      /*一页多少条*/
      pageSize: 10,
      /*一共多少条数据*/
      totalDataNumber: 0,
      /*当前是第几页*/
      curPage: 1,
      searchForm: {
        picks: '',
        category_id: '',
        keys: '',
      },
      dialogFormVisible: false,
      type: 1,
      addForm: {
        category_id: '',
        id: '',
        ids: '',
        rate: '',
      },
      rate: '',
    };
  },
  created() {

    this.curPage = localStorage.getItem('wps_product_page') || 1;
    localStorage.removeItem('wps_product_page')
    this.getData();
    this.getCategory();
    this.getParams();
  },
  methods: {
    checkSelectable(row) {
      return row.check_sta == '未选入'
    },
    handleClick(e) {
      this.getData();
    },
    /*获取配置数据*/
    getParams() {
      let self = this;
      SettingApi.getSupplier({}, true).then(res => {
        self.rate = res.data.model.rate
      })
    },
    detailClick(row) {
      localStorage.setItem('wps_product_page', this.curPage);
      this.$router.push({
        path: '/wps/product/info',
        query: {
          id: row.id,
        }
      });
    },
    addClick1() {
      if (!this.addForm.ids) {
        this.$message({
          message: '请先选择商品',
          type: 'error'
        });
        return
      }
      this.type = 1
      this.addForm.rate = this.rate
      this.dialogFormVisible = true
    },
    handleSelectionChange(e) {
      let arr = e.map(item => item.id)
      this.addForm.ids = arr.join(',')
    },
    onclose() {
      this.addForm = {
        category_id: '',
        id: '',
        ids: '',
        rate: '',
      }
    },
    add() {
      let self = this
      if (self.type == 1) {
        self.$refs.addForm.validate(valid => {
          if (valid) {
            PorductApi.yun_product_pl_add(self.addForm, true).then(data => {
              self.$message({
                message: '添加成功',
                type: 'success'
              });
              self.$router.push('/product/product/index');
            })
          } else {
            self.$message({
              message: '请检查必填项是否填写完整',
              type: 'error'
            });
          }
        });
      }
      if (self.type == 2) {
        self.$refs.addForm.validate(valid => {
          if (valid) {
            PorductApi.yun_product_add(self.addForm, true).then(data => {
              self.$message({
                message: '添加成功',
                type: 'success'
              });
              self.$router.push('/product/product/index');
            })
          } else {
            self.$message({
              message: '请检查必填项是否填写完整',
              type: 'error'
            });
          }
        });
      }
    },
    handleChange(e) {
      this.addForm.category_id = e[e.length - 1];
    },
    handleChange1(e) {
      this.searchForm.category_id = e[e.length - 1];
    },
    /*打开添加*/
    addClick(item) {
      this.addForm.id = item.id
      this.addForm.rate = this.rate
      this.type = 2
      this.dialogFormVisible = true
    },
    /*获取列表*/
    getCategory() {
      let self = this;
      PorductApi.productList({}, true)
        .then(data => {
          self.categoryList = data.data.category;
        })
    },
    /*搜索查询*/
    onSubmit() {
      this.curPage = 1;
      this.getData();
    },
    /*选择第几页*/
    handleCurrentChange(val) {
      let self = this;
      self.loading = true;
      self.curPage = val;
      self.getData();
    },

    /*每页多少条*/
    handleSizeChange(val) {
      this.pageSize = val;
      this.getData();
    },
    /*获取列表*/
    getData() {
      let self = this;
      let Params = self.searchForm;
      Params.page = self.curPage;
      Params.list_rows = self.pageSize;
      self.loading = true;
      PorductApi.yun_product_list(Params, true)
        .then(data => {
          self.loading = false;
          self.tableData = data.data.data;
          self.totalDataNumber = data.data.total;
        })
        .catch(error => {
          self.loading = false;
        });
    },

  }

};
</script>

<style></style>
